// 用户路由
const express = require('express')
const userRouter = express.Router()
const {selectUserInfo,selectRecrord,addIncallRecrord,addOutRecrord,allincall,alloutcall,maxCall,totalRecord} = require('../handle')

const {ErrorModel,SuccessModel} = require('../modle/index')
//#region  获取用户信息接口
userRouter.get('/info',async(req,res)=>{
    let name = req.query.name
let result = await selectUserInfo({name:name,password:''})
let results = new SuccessModel(result)
res.send(results)
})
//#endregion


//#region 获取当前用户的记录 通过type判断为消费记录 或者是 收入记录
userRouter.get('/record',async(req,res)=>{

    let id =req.query.id
    let type = req.query.type
    let size = req.query.size
    let page = req.query.page
    let result = await selectRecrord(id,type,size,page)
    let total = await totalRecord(id,type)
    let data = {
        result,
        total:total[0]['COUNT(*)']
    }
   
    res.send(data)
})
//#endregion



//#region  新增收入记录
userRouter.post('/add/incall',async(req,res)=>{
    let result = await addIncallRecrord({id:req.body.id,name:req.body.name,moeny:req.body.moeny,incalltype:req.body.type,incalldate:req.body.date,textarea:req.body.textarea,day:req.body.day})
    res.send(result)
})
//#endregion

//#region 新增支出记录
userRouter.post('/add/outcall',async(req,res)=>{
    let result = await addOutRecrord({id:req.body.id,name:req.body.name,moeny:req.body.moeny,incalltype:req.body.type,incalldate:req.body.date,textarea:req.body.textarea,day:req.body.day})
    res.send(result)
})


//#endregion

//#region 计算收入总和
userRouter.get('/sum/incall',async(req,res)=>{
    let result = await allincall(req.query.id)
    res.send(result)
})
//#endregion

//#region  计算支出总和
userRouter.get('/sum/outcall',async(req,res)=>{
    let result = await alloutcall(req.query.id)
    res.send(result)
})  
//#endregion

//#region 获取当前allincall 和 alloutcall 最大值
userRouter.get('/max',async(req,res)=>{
    let id = req.query.id
    let result = await maxCall(id)
    let allincall = result.res1[0]['MAX(allincall)']
    let alloutcall = result.res2[0]['MAX(alloutcall)']
    let data ={
        allincall,
        alloutcall
    }

    res.send(data)
})
//#endregion



module.exports = userRouter